package com.lorenzogao.pley.ui.fragment

import android.content.Intent
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.View
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemClickListener
import com.lorenzogao.pley.R
import com.lorenzogao.pley.bean.Category
import com.lorenzogao.pley.common.Constant
import com.lorenzogao.pley.ui.activity.CategoryAppActivity
import com.lorenzogao.pley.ui.adapter.CategoryAdapter
import com.lorenzogao.pley.ui.base.BaseFragment
import com.lorenzogao.pley.ui.di.component.AppComponent
import com.lorenzogao.pley.ui.di.component.DaggerCategoryComponent
import com.lorenzogao.pley.ui.di.moduel.CategoryModule
import com.lorenzogao.pley.ui.presenter.CategoryPresenter
import com.lorenzogao.pley.ui.presenter.contract.CategoryContract
import com.lorenzogao.pley.ui.widget.DividerItemDecoration

/**
 * 作者：Lorenzo Gao
 * Date: 2017/12/22
 * Time: 16:31
 * 邮箱：2508719070@qq.com
 * Description:
 */
class CategoryFragment : BaseFragment<CategoryPresenter>(), CategoryContract.CategoryView {


    private lateinit var mAdapter: CategoryAdapter

    private lateinit var mRecyclerView: RecyclerView


    override fun showData(t: List<Category>) {
        mAdapter.addData(t)
    }


    override fun setLayout(): Int {

        return R.layout.template_recyclerview
    }

    override fun initView() {
        initRecyclerView()

        mPresenter.getAllCategory()
    }

    override fun setupActivityComponent(appComponent: AppComponent) {
        DaggerCategoryComponent.builder().appComponent(appComponent).categoryModule(CategoryModule(this)).build().inject(this)
    }

    private fun initRecyclerView() {

        mRecyclerView = getViews().findViewById(R.id.rl_category)

        mRecyclerView.layoutManager = LinearLayoutManager(activity)

        mRecyclerView.addItemDecoration(DividerItemDecoration(activity, DividerItemDecoration.VERTICAL_LIST))



        mAdapter = CategoryAdapter()

        mRecyclerView.adapter = mAdapter

        mRecyclerView.addOnItemTouchListener(object : OnItemClickListener() {
            override fun onSimpleItemClick(adapter: BaseQuickAdapter<*, *>?, view: View?, position: Int) {
                var intent = Intent(activity, CategoryAppActivity::class.java)
                intent.putExtra(Constant.CATEGORY, mAdapter.data[position])
                startActivity(intent)
            }
        })
    }


}